<template>
  <base-breadcrumb>
    <div class="page-detail">
      <title-name
        size="24"
        title="风险要素"
      />
      <a-card v-loading="loading">
        <title-name title="风险信息" />
        <a-form-model
          ref="form1Rules"
          :model="form1"
          :rules="form1Rules"
        >
          <a-row>
            <a-col span="24">
              <a-form-model-item
                label="风险名称"
                prop="riskName"
              >
                <b-input v-model="form1.riskName" />
              </a-form-model-item>
            </a-col>

            <a-col span="24">
              <a-form-model-item
                label="责任部门"
                prop="orgId"
              >
                <SelectDepartment2 v-model="form1.orgId" />
              </a-form-model-item>
            </a-col>
            <a-col span="24">
              <a-form-model-item
                label="风险内容"
                prop="riskContent"
              >
                <b-textarea
                  v-model="form1.riskContent"
                  :max-length="1000"
                />
              </a-form-model-item>
            </a-col>
            <a-col span="24">
              <a-form-model-item label="一般风险">
                <b-textarea v-model="form1.generalRisk" />
              </a-form-model-item>
            </a-col>
            <a-col span="24">
              <a-form-model-item label="较大风险">
                <b-textarea v-model="form1.greaterRisk" />
              </a-form-model-item>
            </a-col>
            <a-col span="24">
              <a-form-model-item label="重大风险">
                <b-textarea v-model="form1.majorRrisk" />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="创建人">
                <b-input
                  v-model="form1.creatorName"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="创建时间">
                <b-input
                  :value="form1.createTime|dateFormat"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card>
      <div class="tar page-btn-right-top">
        <a-button
          type="primary"
          :loading="loading"
          class="mr10p"
          @click="save"
        >
          保存草稿
        </a-button>
        <a-button
          type="primary"
          :loading="loading"
          class="mr10p"
          @click="submit"
        >
          提交
        </a-button>
        <a-button @click="$close">
          关闭
        </a-button>
      </div>
    </div>
  </base-breadcrumb>
</template>

<script>

import otherTypeApi from '@/api/legal/branchContract/otherType'
import elementCheckListApi from '@/api/legal/risk/elementCheckList'

import SelectDepartment2 from '@/views/Legal/risk/elementCheckList/components/SelectDepartment2'

export default {
  name:'a'+Date.now(),
  components: { SelectDepartment2 },
    data() {
        return {
            loading: false,
            addFile: otherTypeApi.addFile(),
            deleteFile: otherTypeApi.deleteFile(),
            findFileList: otherTypeApi.findFileList(),
            form1: {
                // riskName: Date.now() + '',
                // riskContent: Date.now() + '',
                // generalRisk: Date.now() + '',
                // greaterRisk: Date.now() + '',
                // majorRrisk: Date.now() + '',
                // createTime: Date.now(),
                riskName: '',
                riskContent: '',
                generalRisk: '',
                greaterRisk: '',
                majorRrisk: '',
                createTime: Date.now(),
                orgId: ''
            },
            form1Rules: {
                riskName: { required: true, message: '风险名称不能为空', trigger: 'blur' },
                orgId: { required: true, message: '责任部门不能为空', trigger: 'change' },
                riskContent: { required: true, message: '风险内容不能为空', trigger: 'change' },
                generalRisk: { required: true, message: '一般风险不能为空', trigger: 'blur' },
                greaterRisk: { required: true, message: '较大风险不能为空', trigger: 'blur' },
                majorRrisk: { required: true, message: '重大风险不能为空', trigger: 'change' }
            }
        }
    },
    computed: {
        isShowOptions() {
            return true//(!this.form1.auditStatus) || this.form1.auditStatus === 'DRAFT' || this.form1.auditStatus === 'REJECTED'
        }
    },
    created: async function() {
        if (this.$route.query.id) {
            this.form1.id = this.$route.query.id
            await this.getBaseInfo()
        } else {
            let { name, phone } = this.$userInfo().employee
            this.form1.creatorName = name
            this.form1.creatorPhone = phone
        }
    },
    methods: {
        async getBaseInfo() {
            this.loading = true
            let res = await elementCheckListApi.findListByMainId({}, { id: this.form1.id })
            if (res.code === this.SUCCESS_CODE) {
                this.form1 = res.data
            }
            this.loading = false
        },
        async save() {
            return new Promise(((resolve, reject) => {
                this.$refs.form1Rules.validate(async valid => {
                    if (valid) {
                        this.loading = true
                        let res
                        this.form1.auditStatus = 'DRAFT'
                        if (this.form1.id) {
                            res = await elementCheckListApi.editByRiskFactors(this.form1)
                        } else {
                            res = await elementCheckListApi.createByRiskFactors(this.form1)
                        }
                        if (res.code === 200) {
                            this.$message.success('保存成功')
                            this.form1.id = res.data.id
                            resolve(true)
                            this.getBaseInfo()
                        }
                        this.loading = false
                    } else {
                        this.$noticeWarning('请填写完整')
                    }
                    resolve(false)
                })
            })).then()
        },
        async submit() {
            this.$submitConfirm(async () => {
                if (!await this.save()) {
                    return
                }
                this.loading = true
                let res = await elementCheckListApi.submitApply({id:this.form1.id})
                if (res.code === 200) {
                    this.$noticeSuccess('提交成功')
                    this.$router.back()
                }
                this.loading = false
            }, '请确认是否要提交?')
        }
    }
}
</script>
